<template>
  <div>
    <el-button v-show="listShow" style="margin-top: 10px;" @click="addConferenceRoomManagement()">添加会议室</el-button>
    <el-table
      v-show="listShow"
      width="1138px"
      style="margin-top: 10px;"
      :data="list"
      border
    >
      <el-table-column align="center" width="160px" label="序号">
        <template slot-scope="scope">
          {{ scope.row.roomID }}
        </template>
      </el-table-column>
      <el-table-column label="会议室名称" width="160px" align="center">
        <template slot-scope="scope">
          {{ scope.row.roomName }}
        </template>
      </el-table-column>
      <el-table-column align="center" width="550px" prop="created_at" label="会议室描述">
        <template slot-scope="scope">
          <span>{{ scope.row.description }}</span>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" width="268px" label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="small"
            @click="xg(scope.row.roomID)">
            修改
          </el-button>
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.row.roomID)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-show="listShow"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="this.pageNum"
      :page-size="this.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="this.total"
      :page-sizes="[5, 10, 20]"
    />

    <!--  修改  -->
    <el-form
      :model="updateMeetingRoomsList"
      :rules="rules"
      ref="formRef"
      v-show="updatelistLoading"
      label-width="auto"
      style="max-width: 600px;margin-top: 10px">
      <el-form-item
        label="会议室名称"
        prop="id"
      >
        <el-input
          v-model="updateMeetingRoomsList.roomName"
          type="text"
          autocomplete="off"
        />
      </el-form-item>

      <el-form-item
        label="会议室描述"
        prop="purchasingprice"
      >
        <el-input
          v-model="updateMeetingRoomsList.description"
          type="textarea"
          autocomplete="off"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="danger" @click="updateForm()">修改</el-button>
        <el-button @click="qx()">返回</el-button>
      </el-form-item>
    </el-form>

    <!--  新增  -->
    <el-form
      :model="addMeetingRoomsList"
      v-show="addlistLoading"
      :rules="rules"
      ref="formRef"
      label-width="auto"
      style="max-width: 600px;margin-top: 10px">

      <el-form-item
        label="会议室名称"
        prop="roomName"
      >
        <el-input
          v-model="addMeetingRoomsList.roomName"
          type="text"
          autocomplete="off"
        />
      </el-form-item>

      <el-form-item
        label="会议室描述"
        prop="description"
      >
        <el-input
          v-model="addMeetingRoomsList.description"
          type="textarea"
          autocomplete="off"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="danger" @click="submitForm">保存</el-button>
        <el-button @click="qx()">返回</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>
<script>
import {
  addMeetingRooms,
  deleteMeetingRooms,
  getConferenceRoomManagement,
  getMeetingRoomsId, updateMeetingRooms
} from "@/api/conferenceRoom/ConferenceRoomManagement/table";
import {announcementsGetId, submitForm} from "@/api/table";

export default {
  data() {
    return {
      total: 0,
      pages: 0,
      pageSize: 5,
      pageNum: 1,
      list: [],
      updateMeetingRoomsList: [],
      addMeetingRoomsList: {},
      updatelistLoading: false,
      addlistLoading: false,
      listShow: false,
      rules: {
        roomName: [
          {required: true, message: '必填', trigger: 'blur'}
        ],
        description: [
          {required: true, message: '必填', trigger: 'blur'}
        ],
      },
    }
  },

  created() {
    this.fetchData();
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.fetchData();
    },
    fetchData() {
      this.updatelistLoading = false
      this.addlistLoading = false
      this.listShow = true
      getConferenceRoomManagement(this.pageNum, this.pageSize).then(response => {
        this.total = response.data.total
        this.pages = response.data.pages
        this.list = response.data.list
      })
    },
    handleDelete(id) {
      if (confirm("是否删除")) {
        deleteMeetingRooms(id).then(data => {
          console.log(data)
          if (data.data.result) {
            alert("删除成功")
          } else {
            alert("删除失败")
          }
          window.location.reload();
        })
      }
    },
    xg(id) {
      this.updatelistLoading = true
      this.addlistLoading = false
      this.listShow = false
      getMeetingRoomsId(id).then(data => {
        this.updateMeetingRoomsList = data.data
      })
    },
    updateForm() {
      updateMeetingRooms(this.updateMeetingRoomsList).then(data => {
        if (data.data.result) {
          alert("修改成功")
        } else {
          alert("修改失败")
        }
        window.location.reload();
      })
    },
    qx() {
      this.updatelistLoading = false
      this.addlistLoading = false
      this.listShow = true
      window.location.reload();
    },
    addConferenceRoomManagement() {
      this.updatelistLoading = false
      this.addlistLoading = true
      this.listShow = false
    },
    submitForm() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          addMeetingRooms(this.addMeetingRoomsList).then(data => {
            console.log(data)
            if (data.data.result) {
              alert("新增成功")
            } else {
              alert("新增失败")
            }
            window.location.reload();
          })
        }else {
          alert("新增失败")
          return false;
        }
      })
    }
  }
}
</script>

